package com.cn.wanxi.dao;

import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.util.GetSqlSession;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class NewsImpl implements NewsDao {
    private SqlSession sqlSession = GetSqlSession.getSqlSession();//连接数据库
    public Integer add(NewsModel newsModel) {
        int a = sqlSession.insert("com.cn.wanxi.dao.NewsDao.add", newsModel);
        GetSqlSession.commit();
        return a;
    }

    /**
     * 修改新闻数据
     *
     * @param newsModel
     * @return
     */
    public Integer update(NewsModel newsModel) {
        int a = sqlSession.insert("com.cn.wanxi.dao.NewsDao.update", newsModel);
        GetSqlSession.commit();
        return a;
    }

    public Integer delete(NewsModel newsModel) {
        int a = sqlSession.insert("com.cn.wanxi.dao.NewsDao.delete", newsModel);
        GetSqlSession.commit();
        return a;
    }

    /**
     * 根据id查找新闻信息
     *
     * @param id
     * @return
     */
    public NewsModel findById(Integer id) {
        NewsModel newsModel = sqlSession.selectOne("com.cn.wanxi.dao.NewsDao.findById", id);
        GetSqlSession.commit();
        return newsModel;
    }

    public List<NewsModel> findAll() {
        List<NewsModel> list = sqlSession.selectList("com.cn.wanxi.dao.NewsDao.findAll");
        GetSqlSession.commit();
        return list;
    }

    /**
     * 分页
     *
     * @param pageModel
     * @return
     */
    public List<NewsModel> findPage(PageModel pageModel) {
        SqlSession sqlSession = GetSqlSession.getSqlSession();//连接数据库
        List<NewsModel> list = sqlSession.selectList("com.cn.wanxi.dao.NewsDao.findPage", pageModel);
        GetSqlSession.commit();
        return list;
    }

    /**
     * 返回总条数
     * @param pageModel
     * @return
     */
    public Integer findCount(PageModel pageModel) {
        SqlSession sqlSession = GetSqlSession.getSqlSession();//连接数据库
        Integer count = sqlSession.selectOne("com.cn.wanxi.dao.NewsDao.findCount", pageModel);
        GetSqlSession.commit();
        return count;
    }

    @Override
    public List<NewsModel> findNews(NewsModel newsModel) {
        SqlSession sqlSession = GetSqlSession.getSqlSession();//连接数据库
        List<NewsModel> list = sqlSession.selectList("com.cn.wanxi.dao.NewsDao.findNews", newsModel);
        GetSqlSession.commit();
        return list;
    }
}
